package com.baoxin.chenxixc4coach.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.GlideApp;
import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.bean.Potential;
import com.baoxin.chenxixc4coach.utils.MyLog;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

public class PotentialAdapter extends RecyclerView.Adapter<PotentialAdapter.ViewHolder> {

    private List<Potential> potentialList;
    private Context context;
    private CallListener callListener;
    private EditListener editListener;

    public PotentialAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<Potential> potentialList) {
        this.potentialList = potentialList;
    }

    public void setCallListener(CallListener callListener) {
        this.callListener = callListener;
    }

    public void setEditListener(EditListener editListener) {
        this.editListener = editListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.potential_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final Potential p = potentialList.get(position);
        GlideApp.with(context)
                .load(p.getCtConPhoto())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .error(R.drawable.default_)
                .into(holder.icon);
        holder.name.setText(p.getCtConTrname());
        holder.time.setText(p.getCtConTime());

        String remarkTx = p.getCtConRemark();
        if (remarkTx != null) {
            holder.remark.setText("备注：" + remarkTx);
            holder.remark.setVisibility(View.VISIBLE);
        } else {
            holder.remark.setVisibility(View.GONE);
        }


        int state = p.getCtConState();
        MyLog.d("getCtConState",""+state);
        switch (state) {
            case 0:
                holder.call.setBackgroundResource(R.drawable.gj_call);
                holder.call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        callListener.call(potentialList.get(position).getCtConTrphone());
                    }
                });
                break;
            case 1:
                holder.call.setText("继续跟进");
                holder.call.setBackgroundResource(R.drawable.gj_continue);
                holder.call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        callListener.call(potentialList.get(position).getCtConTrphone());
                    }
                });
                break;
            case 2:
                holder.call.setText("确定报名");
                holder.call.setBackgroundResource(R.drawable.gj_certain);
                break;
            case 3:
                holder.call.setText("结束跟进");
                holder.call.setBackgroundResource(R.drawable.gj_end);
                break;
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editListener.editRecord(p);
            }
        });


    }

    @Override
    public int getItemCount() {
        return potentialList == null ? 0 : potentialList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        View itemView;
        ImageView icon;
        TextView name;
        TextView time;
        Button call;
        TextView remark;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            icon = itemView.findViewById(R.id.icon);
            name = itemView.findViewById(R.id.name);
            time = itemView.findViewById(R.id.time);
            call = itemView.findViewById(R.id.call);
            remark = itemView.findViewById(R.id.remark);

        }
    }

    public interface CallListener {
        void call(String phone);
    }

    public interface EditListener {
        void editRecord(Potential p);
    }
}
